import { Bus } from "../Bus";
import { ButtonType } from "../Input/Controller";

/**1P按键控制 */
export const enum Keys {
	//1P 按键
	Up_1p = "KeyW",			//W
	Down_1p = "KeyS",		//S
	Left_1p = "KeyA",		//A
	Right_1p = "KeyD",		//D
	Select_1p = "KeyZ",		//Z
	Start_1p = "KeyX",		//X
	B_1p = "KeyG",			//G
	A_1p = "KeyH",			//H
	TB_1p = "KeyT",			//T
	TA_1p = "KeyY",			//Y

	//2P 按键
	Up_2p = "ArrowUp",			//W
	Down_2p = "ArrowDown",		//S
	Left_2p = "ArrowLeft",		//A
	Right_2p = "ArrowRight",	//D
	Select_2p = "Numpad1",		//Z
	Start_2p = "Numpad2",		//X
	B_2p = "Numpad4",			//G
	A_2p = "Numpad5",			//H
	TB_2p = "Numpad7",			//T
	TA_2p = "Numpad8"			//Y
}

export enum InputType {
	StandardController = 1
}

export class Input {

	private inputType: InputType = InputType.StandardController;
	private readonly bus: Bus;

	constructor(bus: Bus) {
		this.bus = bus;
		window.addEventListener("keydown", (ev) => {
			this.CheckStandardController(ev, true);
		});

		window.addEventListener("keyup", (ev) => {
			this.CheckStandardController(ev, false);
		});
	}

	CheckStandardController(ev: KeyboardEvent, isKeyDown: boolean) {
		let player = 0;
		switch (ev.code) {
			case Keys.A_1p:
				this.bus.controller.buttonStatue[0][ButtonType.A] = isKeyDown;
				break;
			case Keys.B_1p:
				this.bus.controller.buttonStatue[0][ButtonType.B] = isKeyDown;
				break;
			case Keys.Select_1p:
				this.bus.controller.buttonStatue[0][ButtonType.Select] = isKeyDown;
				break;
			case Keys.Start_1p:
				this.bus.controller.buttonStatue[0][ButtonType.Start] = isKeyDown;
				break;
			case Keys.Up_1p:
				this.bus.controller.buttonStatue[0][ButtonType.Up] = isKeyDown;
				break;
			case Keys.Down_1p:
				this.bus.controller.buttonStatue[0][ButtonType.Down] = isKeyDown;
				break;
			case Keys.Left_1p:
				this.bus.controller.buttonStatue[0][ButtonType.Left] = isKeyDown;
				break;
			case Keys.Right_1p:
				this.bus.controller.buttonStatue[0][ButtonType.Right] = isKeyDown;
				break;

			case Keys.A_2p:
				this.bus.controller.buttonStatue[1][ButtonType.A] = isKeyDown;
				break;
			case Keys.B_2p:
				this.bus.controller.buttonStatue[1][ButtonType.B] = isKeyDown;
				break;
			case Keys.Select_2p:
				this.bus.controller.buttonStatue[1][ButtonType.Select] = isKeyDown;
				break;
			case Keys.Start_2p:
				this.bus.controller.buttonStatue[1][ButtonType.Start] = isKeyDown;
				break;
			case Keys.Up_2p:
				this.bus.controller.buttonStatue[1][ButtonType.Up] = isKeyDown;
				break;
			case Keys.Down_2p:
				this.bus.controller.buttonStatue[1][ButtonType.Down] = isKeyDown;
				break;
			case Keys.Left_2p:
				this.bus.controller.buttonStatue[1][ButtonType.Left] = isKeyDown;
				break;
			case Keys.Right_1p:
				this.bus.controller.buttonStatue[1][ButtonType.Right] = isKeyDown;
				break;
		}
	}
}